package com.powernode.service;

import com.powernode.dao.DormitoryAdminMapper;
import com.powernode.dao.FloorAdminMapper;
import com.powernode.dao.IsolatedManMapper;
import com.powernode.pojo.DormitoryAdmin;
import com.powernode.pojo.FloorAdmin;
import com.powernode.pojo.IsolatedMan;
import com.powernode.utils.ConstantUtil;
import com.powernode.utils.Md5Util;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class IsolatedManService {
    @Resource
    public IsolatedManMapper isolatedManMapper;
    @Resource
    public DormitoryAdminMapper dormitoryAdminMapper;
    @Resource
    public FloorAdminMapper floorAdminMapper;



    public int count(Map<String, Object> param) {
        return isolatedManMapper.count(param);
    }

    public List<IsolatedMan> getPageResult(Map<String, Object> param) {
        return isolatedManMapper.selectPageResult(param);
    }

    /**
     * 修改 的 主要 算法 逻辑
     * @param entity： 传进来的隔离者信息
     */
    public void modify(IsolatedMan entity){
        if(entity.getId()!=null) {
            IsolatedMan isolatedMan = isolatedManMapper.selectById(entity.getId());//修改前，先拿到隔离者信息，保证安全
            isolatedManMapper.update(entity);//先修改，保证外键
            //TODO 修改寝室长逻辑/修改楼层长逻辑  (如果原本是寝室长，改了角色，就要在寝室长表中删除)
            //那么，问题来了，要是修改isolatedMan的角色同时，也修改了其他信息，那其他信息就会更改失败，相当于比如寝室长中多出一个学号或者其他信息不对的寝室长
//            IsolatedMan isolatedMan = isolatedManMapper.selectById(entity.getId());//拿到隔离者信息
            String xuehao = entity.getStudentNumber();//获取此隔离者学号
            String oldRole = isolatedMan.getRole();//获取隔离者 旧身份
            String name = entity.getName();//隔离者名字
            String address = entity.getSite();//寝室编号
            String phone = entity.getPhone();//隔离者手机号

            String newRole = entity.getRole(); //新身份

            if(newRole.equals("寝室长")){//如果新角色是：寝室长
                DormitoryAdmin dormitoryAdmin = dormitoryAdminMapper.selectByStudentNumber(xuehao);
                DormitoryAdmin dormitoryAdmin1 = new DormitoryAdmin();
                dormitoryAdmin1.setStudentNumber(xuehao);
                dormitoryAdmin1.setName(name);
                dormitoryAdmin1.setSite(address);
                dormitoryAdmin1.setPhone(phone);
                dormitoryAdmin1.setIspost(2);
                dormitoryAdmin1.setPassword(String.valueOf(ConstantUtil.PASSWORD.code));
                if(dormitoryAdmin==null){//添加寝室长（旧角色不是寝室长）
                    dormitoryAdminMapper.insert(dormitoryAdmin1);
                }else {//更新寝室长（旧角色是寝室长）
                    dormitoryAdmin1.setId(dormitoryAdmin.getId());
                    dormitoryAdmin1.setIspost(dormitoryAdmin.getIspost());
                    dormitoryAdmin1.setPassword(dormitoryAdmin.getPassword());
                    dormitoryAdminMapper.update(dormitoryAdmin1);
                }
                if (oldRole.equals("楼层长")){
                    //TODO 删除楼层长
                    FloorAdmin floorAdmin = floorAdminMapper.getByStudentNumber(xuehao);
                    Integer[] id = {floorAdmin.getId()};
                    floorAdminMapper.deletes(id);
                }
            }
            if(newRole.equals("楼层长")){//如果新角色是：楼层长
                FloorAdmin floor = floorAdminMapper.getByStudentNumber(xuehao);//查一下这个学生，修改前，旧角色是不是楼层长？
                FloorAdmin floor1 = new FloorAdmin();
                floor1.setName(address);
                floor1.setName(name);
                floor1.setStudentNumber(xuehao);
                floor1.setPhone(phone);
                floor1.setFloorSite(address.substring(0,5));
                floor1.setIspost(2);
                floor1.setPassword(Md5Util.encode(String.valueOf(ConstantUtil.PASSWORD.code)));
                if(floor==null){//添加楼层长（旧角色不是楼层长）
                    floorAdminMapper.insert(floor1);
                }else {//更新楼层长（旧角色是楼层长）
                    floor1.setId(floor.getId());
                    floor1.setIspost(floor.getIspost());
                    floor1.setPassword(floor.getPassword());
                    floorAdminMapper.update(floor1);
                }
                if (oldRole.equals("寝室长")){
                    //TODO 删除寝室长
                    DormitoryAdmin dormitoryAdmin = dormitoryAdminMapper.selectByStudentNumber(xuehao);
                    Integer[] id = {dormitoryAdmin.getId()};
                    dormitoryAdminMapper.deletes(id);
                }
            }
//            isolatedManMapper.update(entity);
        } else {
            //TODO 添加寝室长逻辑/添加楼层长逻辑
            isolatedManMapper.insert(entity);
            if (entity.getRole().equals("寝室长")){
                //添加寝室长
                DormitoryAdmin dormitoryAdmin = new DormitoryAdmin();
                dormitoryAdmin.setStudentNumber(entity.getStudentNumber());
                dormitoryAdmin.setName(entity.getName());
                dormitoryAdmin.setSite(entity.getSite());
                dormitoryAdmin.setPhone(entity.getPhone());
                dormitoryAdmin.setIspost(2);
                dormitoryAdmin.setPassword(Md5Util.encode(String.valueOf(ConstantUtil.PASSWORD.code)));
                dormitoryAdminMapper.insert(dormitoryAdmin);
            }else if (entity.getRole().equals("楼层长")){
                //添加楼层长
                FloorAdmin floorAdmin = new FloorAdmin();
                floorAdmin.setFloorSite(entity.getSite());
                floorAdmin.setName(entity.getName());
                floorAdmin.setStudentNumber(entity.getStudentNumber());
                floorAdmin.setPhone(entity.getPhone());
                floorAdmin.setIspost(2);
                floorAdmin.setPassword(Md5Util.encode(String.valueOf(ConstantUtil.PASSWORD.code)));
                floorAdminMapper.insert(floorAdmin);
            }
        }
    }

    public void add(IsolatedMan entity) {
        isolatedManMapper.insert(entity);
    }

    public IsolatedMan get(Integer id) {
        return isolatedManMapper.selectById(id);
    }

    public void removes(Integer[] ids) {
        List<String> xuehaoList = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            xuehaoList.add(isolatedManMapper.selectById(ids[i]).getStudentNumber());
        }
        floorAdminMapper.deletesByStudentNumber(xuehaoList.toArray(new String[xuehaoList.size()]));//删除寝室长
        dormitoryAdminMapper.deletesByStudentNumber(xuehaoList.toArray(new String[xuehaoList.size()]));//删除楼层长
        isolatedManMapper.deletes(ids);
    }

    public IsolatedMan selectByStudentNumber(String studentNumber) {
        return isolatedManMapper.selectByStudentNumber(studentNumber);
    }
}
